package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ComDetails;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 企业详情Controller
 *
 * @author wen
 * @date 2021-09-11
 */
@RestController
@RequestMapping("/system/details")
public class ComDetailsController extends BaseController {
    @Autowired
    private IComDetailsService comDetailsService;

    @Autowired
    private IProvincesService provincesService;

    @Autowired
    private ICitiesService citiesService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IAreasService areasService;



    /**
     * 查询企业详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(ComDetails comDetails) {
        startPage();
        List<ComDetails> list = comDetailsService.selectComDetailsList(comDetails);
        return getDataTable(list);
    }

    /**
     * 导出企业详情列表
     */
    @PreAuthorize("@ss.hasPermi('system:details:export')")
    @Log(title = "企业详情", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ComDetails comDetails) {
        List<ComDetails> list = comDetailsService.selectComDetailsList(comDetails);
        ExcelUtil<ComDetails> util = new ExcelUtil<ComDetails>(ComDetails.class);
        return util.exportExcel(list, "企业详情数据");
    }

    /**
     * 获取企业详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @GetMapping(value = "/{cId}")
    public AjaxResult getInfo(@PathVariable("cId") Long cId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put(AjaxResult.DATA_TAG, comDetailsService.selectComDetailsByCId(cId));
        return ajax;
    }

    /**
     * 获取企业详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @GetMapping(value = "/comName/{coId}")
    public AjaxResult getInfoCompanyName(@PathVariable("coId") String coId) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("companiesName", companyService.selectCompanyByComId(Long.parseLong(coId)).getComName());
        return ajax;
    }

    /**
     * 查看企业实名详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:query')")
    @Log(title = "详情查询", businessType = BusinessType.GRANT)
    @GetMapping("/companies/{cId}")
    public AjaxResult selectCompanies(@PathVariable Long cId)
    {
        return AjaxResult.success(comDetailsService.selectComDetailsByCId(cId));
    }

    /**
     * 获取未实名公司列表
     */
    @GetMapping("/companyName")
    public AjaxResult getInfoCompanyName() {
        return AjaxResult.success(companyService.selectCompanyName());
    }
    /**
     * 获取省列表
     */
    @GetMapping("/proCom/{cPro}")
    public AjaxResult getProCompany(@PathVariable String cPro) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("provincIds", provincesService.selectProById(cPro));
        return ajax;
    }

    @GetMapping("/proCity/{cCity}")
    public AjaxResult getCityCompany(@PathVariable String cCity) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("cityIds", citiesService.selectCityById(cCity));
        return ajax;
    }

    @GetMapping("/proArea/{cArea}")
    public AjaxResult getAreaCompany(@PathVariable String cArea) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("areaIds", areasService.selectAreaById(cArea));
        return ajax;
    }

    /**
     * 获取省列表
     */
    @GetMapping("/pro")
    public AjaxResult getInfoPro() {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("provincs", provincesService.selectProvincesAll());
        return ajax;
    }

    /**
     * 获取省列表
     */
    @GetMapping("/cit/{cTy}")
    public AjaxResult getInfoCity(@PathVariable String cTy) {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("citys", citiesService.selectCitiesByProId(cTy));
        return ajax;
    }

    /**
     * 新增企业详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:add')")
    @Log(title = "企业详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ComDetails comDetails) {
        companyService.updateCompanyStatus(comDetails.getCoId());
        return toAjax(comDetailsService.insertComDetails(comDetails));
    }

    /**
     * 修改企业详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:edit')")
    @Log(title = "企业详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ComDetails comDetails) {
        return toAjax(comDetailsService.updateComDetails(comDetails));
    }

    /**
     * 删除企业详情
     */
    @PreAuthorize("@ss.hasPermi('system:details:remove')")
    @Log(title = "企业详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cIds}")
    public AjaxResult remove(@PathVariable Long[] cIds) {
        return toAjax(comDetailsService.deleteComDetailsByCIds(cIds));
    }

}
